{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71597ba3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#installations\n",
    "%pip install clip\n",
    "%pip install torch\n",
    "%pip install pillow\n",
    "%pip install faiss-cpu\n",
    "%pip install numpy\n",
    "%pip install git+https://github.com/openai/CLIP.git\n",
    "%pip install openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e927e05e",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6608fe63",
   "metadata": {},
   "outputs": [],
   "source": [
    "# model imports\n",
    "import faiss\n",
    "import json\n",
    "import torch\n",
    "from openai import OpenAI\n",
    "import torch.nn as nn\n",
    "from torch.utils.data import DataLoader\n",
    "import clip\n",
    "client = OpenAI()\n",
    "\n",
    "# helper imports\n",
    "from tqdm import tqdm\n",
    "import json\n",
    "import os\n",
    "import numpy as np\n",
    "import pickle\n",
    "from typing import List, Union, Tuple\n",
    "\n",
    "# visualisation imports\n",
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "import base64"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "db817764",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████| 338M/338M [00:57<00:00, 6.19MiB/s]\n"
     ]
    }
   ],
   "source": [
    "#load model on device. The device you are running inference/training on is either a CPU or GPU if you have.\n",
    "device = \"cpu\"\n",
    "model, preprocess = clip.load(\"ViT-B/32\",device=device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1e25b497",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_image_paths(directory: str, number: int = None) -> List[str]:\n",
    "    image_paths = []\n",
    "    count = 0\n",
    "    for filename in os.listdir(directory):\n",
    "        if filename.endswith('.jpeg'):\n",
    "            image_paths.append(os.path.join(directory, filename))\n",
    "            if number is not None and count == number:\n",
    "                return [image_paths[-1]]\n",
    "            count += 1\n",
    "    return image_paths\n",
    "direc = 'img/'\n",
    "image_paths = get_image_paths(direc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e79533ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['img/train17.jpeg', 'img/train2.jpeg']\n"
     ]
    }
   ],
   "source": [
    "print(image_paths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4bc68cbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_features_from_image_path(image_paths):\n",
    "  images = [preprocess(Image.open(image_path).convert(\"RGB\")) for image_path in image_paths]\n",
    "  image_input = torch.tensor(np.stack(images))\n",
    "  with torch.no_grad():\n",
    "    image_features = model.encode_image(image_input).float()\n",
    "  return image_features\n",
    "image_features = get_features_from_image_path(image_paths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f758867c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 512])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "torch.Tensor"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n",
      "\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n",
      "\u001b[1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. \n",
      "\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
     ]
    }
   ],
   "source": [
    "print(image_features.shape)\n",
    "type(image_features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8b636551",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = faiss.IndexFlatIP(image_features.shape[1])\n",
    "index.add(image_features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "abe7d510",
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'description.json'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mFileNotFoundError\u001b[39m                         Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[15]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m      1\u001b[39m data = []\n\u001b[32m      2\u001b[39m image_path = \u001b[33m'\u001b[39m\u001b[33mtrain1.jpeg\u001b[39m\u001b[33m'\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mdescription.json\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mr\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m file:\n\u001b[32m      4\u001b[39m     \u001b[38;5;28;01mfor\u001b[39;00m line \u001b[38;5;129;01min\u001b[39;00m file:\n\u001b[32m      5\u001b[39m         data.append(json.loads(line))\n",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\RAG\\MultiModel\\.venv\\Lib\\site-packages\\IPython\\core\\interactiveshell.py:343\u001b[39m, in \u001b[36m_modified_open\u001b[39m\u001b[34m(file, *args, **kwargs)\u001b[39m\n\u001b[32m    336\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[32m0\u001b[39m, \u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m}:\n\u001b[32m    337\u001b[39m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[32m    338\u001b[39m         \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mIPython won\u001b[39m\u001b[33m'\u001b[39m\u001b[33mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m by default \u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m    339\u001b[39m         \u001b[33m\"\u001b[39m\u001b[33mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m    340\u001b[39m         \u001b[33m\"\u001b[39m\u001b[33myou can use builtins\u001b[39m\u001b[33m'\u001b[39m\u001b[33m open.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m    341\u001b[39m     )\n\u001b[32m--> \u001b[39m\u001b[32m343\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mio_open\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[31mFileNotFoundError\u001b[39m: [Errno 2] No such file or directory: 'description.json'"
     ]
    }
   ],
   "source": [
    "data = []\n",
    "image_path = 'train1.jpeg'\n",
    "with open('description.json', 'r') as file:\n",
    "    for line in file:\n",
    "        data.append(json.loads(line))\n",
    "def find_entry(data, key, value):\n",
    "    for entry in data:\n",
    "        if entry.get(key) == value:\n",
    "            return entry\n",
    "    return None"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
